function ties
% test the tie-breaking rule:
% 2 items, n agents.
% x discrete bids


% test using uniform distribution
% for each auction
%   choose a bid for each agent uar
%   our bid is fixed
%   find the winning bid, and if the same as our bid,
%   count the number of ties and
%   flip a corresponding coin to see if we won.
% 
% repeat t times - this will give us empirical prob of winning with bid b
% 
% compute the same from the equation


n=5;%total number of agents including us
B=10;%number of bids. bids are integers from 1 to B
bs = 1:1:B;%bids
%myb=[10.01 5];%our bid
myb=[8 6];%our bid

T=100000;%num samples

%estimate the probability of winning both auctions

m=2;%num auctions
allCount=0;
wonCount=0;
%temp = randi(B,[n-1,100])
%mean(temp)
%adfas
d=0


for t=1:T
    %for each auction
    won=zeros(1,m);    
    for a=1:m
        myb(a);
        %t random bids of other agents for the auction
        bids = randi(B,[n-1,1]);
        p = max(bids);%price
        if myb(a) == p
            numTies = sum(bids == p);
            r = rand(1);
            %did we win?
            if (1.0/(numTies+1)) >  r
                won(a)=1;
%                 display 'won a tie';
            else
%                 display 'lost a tie';
            end
        elseif myb(a) > p
                won(a)=1;
%                 display 'highest bid';
        end
    end
    allCount = allCount+1;
    if(sum(won) == m)
        wonCount = wonCount+1;
%         display 'won both';
    end
end
allCount

computedProb = wonCount/allCount;
display 'computed prob: '
computedProb

format long
s=0;
mu = myb(1);
nu = myb(2);
for x=0:n-1
    for y=0:n-1-x
        for z=0:n-1-x-y
            
            term = nchoosek(n-1,x)*nchoosek(n-1-x,y)*nchoosek(n-1-x-y,z)*(1/((x+z+1)*(y+z+1)))...
                *((1/B)*((nu-1)/B))^x ...
                *((1/B)*((mu-1)/B))^y ...
                *((1/B)*(1/B))^z ...
                *(((mu-1)/B)*((nu-1)/B))^(n-1-x-y-z);
        
            s = s + term;
        end
    end
end
display 'equation prob'
s



